app.controller('FileUploadCtrl', ['$scope', 'FileUploader','toaster','util', function($scope, FileUploader,toaster,util) {
    var uploader = $scope.uploader = new FileUploader({
        url: encodeURL('/file/ftp/upload')
    });
    
    var $toaster = toaster;
    
    var modular = {
    	init :function(){
    		this.loadFiles();
    		this.bindEvt();
    	},
    	bindEvt :function(){
    		$('#rootFolder').click(function(){
    			modular.loadFiles();
    		});
    	},
    	fileClick :function(file){
    		if(!file){
    			modular.loadFiles();
    		}
    		if(file.isDirectory){
    			$scope.currentPath = file.filePath;
    			modular.loadFiles(file.filePath);
    		} 
    	},
    	fileDel :function(file){
    		if(file){
    			util.confirm("do you realy want to delete these items?", function(){
    				modular.delFile(file);
        		});
    		}
    	},
    	fileAdd:function(currentPath,name){
    		if(currentPath == null || name == undefined){
    			currentPath = "";
    		}
    		if(name == null || name == undefined){
    			util.warning('提示', '请先填写文件夹名称');
    			return;
    		}
    		$.ajax({
    			url : encodeURL('/file/ftp/mkdir'),
    			type :'post',
    			data :{parentPath:currentPath,name:name},
    			dataType :'json',
    			success :function(result){
    				if(result.success){
    					modular.loadFiles($scope.currentPath);
    				}else{
    					$toaster.pop('error', 'title', result.i18nMessage);
    				}
    			}
    		});
    	},
    	delFile:function(file){
    		$.ajax({
    			url : encodeURL('/file/ftp/delete'),
    			type :'post',
    			data :{path :file.filePath,type:file.type},
    			dataType :'json',
    			success :function(result){
    				if(result.success){
    					modular.loadFiles($scope.currentPath);
    				}else{
    					$toaster.pop('error', 'title', result.i18nMessage);
    				}
    			}
    		});
    	},
    	loadFiles :function(remotePath){
    		$.ajax({
    			url : encodeURL('/file/ftp/list'),
    			type :'post',
    			data :{path :remotePath},
    			dataType :'json',
    			success :function(result){
    				if(result.success){
    					$scope.files = result.datas.list;
    					$scope.$apply();
    				}else{
    					$toaster.pop('error', 'title', result.i18nMessage);
    				}
    			}
    		});
    	}
    };
    
    modular.init();
    
    $scope.fileClick = modular.fileClick;
    $scope.fileDel = modular.fileDel;
    $scope.fileAdd = modular.fileAdd;

    // FILTERS

    uploader.filters.push({
        name: 'customFilter',
        fn: function(item /*{File|FileLikeObject|Folder}*/, options) {
            return this.queue.length < 10;
        }
    });

    // CALLBACKS

    uploader.onWhenAddingFileFailed = function(item /*{File|FileLikeObject|Folder}*/, filter, options) {
        console.info('onWhenAddingFileFailed', item, filter, options);
    };
    uploader.onAfterAddingFile = function(fileItem) {
        console.info('onAfterAddingFile', fileItem);
    };
    uploader.onAfterAddingAll = function(addedFileItems) {
        console.info('onAfterAddingAll', addedFileItems);
    };
    uploader.onBeforeUploadItem = function(item) {
        console.info('onBeforeUploadItem', item);
        //添加目录路径
        if($scope.currentPath != null && $scope.currentPath != undefined){
        	item.file.name = $scope.currentPath + "/" + item._file.name ;
        }
    };
    uploader.onProgressItem = function(fileItem, progress) {
        console.info('onProgressItem', fileItem, progress);
    };
    uploader.onProgressAll = function(progress) {
        console.info('onProgressAll', progress);
    };
    uploader.onSuccessItem = function(fileItem, response, status, headers) {
        console.info('onSuccessItem', fileItem, response, status, headers);
        modular.loadFiles($scope.currentPath);
    };
    uploader.onErrorItem = function(fileItem, response, status, headers) {
        console.info('onErrorItem', fileItem, response, status, headers);
    };
    uploader.onCancelItem = function(fileItem, response, status, headers) {
        console.info('onCancelItem', fileItem, response, status, headers);
    };
    uploader.onCompleteItem = function(fileItem, response, status, headers) {
        console.info('onCompleteItem', fileItem, response, status, headers);
    };
    uploader.onCompleteAll = function() {
        console.info('onCompleteAll');
    };

    console.info('uploader', uploader);
}]);